package 剑指offer;

import 分治.CommonUtil;

/**
 * @description:
 * @author: ywk
 * @date: 2021-03-13
 */
public class 奇偶数分离 {
    public static void main(String[] args) {
        int[] arr = {12, 34, 45, 9, 8, 90, 3};
        CommonUtil.printArr(arr);
        DutchNationalFlag(arr, arr.length);
        CommonUtil.printArr(arr);
    }

    //左边偶数，右边奇数
    private static void DutchNationalFlag(int[] arr, int length) {
        int left = 0, right = length - 1;
        while (left < right) {
            while (arr[left] % 2 == 0 && left < right) {
                left++;
            }
            while (arr[right] % 2 != 0 && left < right) {
                right--;
            }
            if (left < right) {
                CommonUtil.swap(arr, left, right);
            }
        }
    }
}
